<template>
  <div class="login_container">
    <div class="login_img">
      <img src="../../assets/img/login.jpg" />
    </div>

    <div class="login_right">
      <el-form class="login_box"
               :rules="loginRules"
               ref="loginFormRef"
               :model="loginForm">
        <el-form-item prop="userId">
          <el-input v-model="loginForm.userId"
                    placeholder="账号"></el-input>
        </el-form-item>
        <el-form-item prop="userPass">
          <el-input v-model="loginForm.userPass"
                    type="password"
                    placeholder="密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="btn_login"
                     @click="login()"
                     type="success">登录</el-button>
        </el-form-item>
        <el-form-item>
          <el-link class="link_register"
                   :underline="false"
                   href="http://localhost:8081/#/register"
                   type="warning">没有账号，注册一个？</el-link>
        </el-form-item>
      </el-form>
    </div>

  </div>
</template>
<script>
import { login } from '../../api/http'
export default {
  data () {
    const isNum = (rule, value, callback) => {
      const id = /^[0-9]*$/
      if (!id.test(value)) {
        callback(new Error('账号只能为数字'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        userId: '',
        userName: '',
        userPass: ''
      },
      // 校验规则
      loginRules: {
        // 校验账号
        userId: [
          { required: true, message: '账号为必填项', trigger: 'blur' },//必填项验证
          { min: 5, max: 12, message: '长度在 5 到 12 个字符', trigger: 'blur' },//长度验证
          { validator: isNum, trigger: 'blur' }
        ],
        // 校验密码
        userPass: [
          { required: true, message: '密码为必填项', trigger: 'blur' },//必填项验证
          { min: 5, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }//长度验证
        ]
      }
    }
  },
  methods: {
    async login () {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return;
        const { data: res } = await login(this.loginForm);
        console.log(res.data);
        if (res.code != 200) {
          this.$message.error("登录失败"); //信息提示
          return;
        }
        window.sessionStorage.setItem("userId", res.data.userId);
        this.$message.success("登录成功"); //信息提示
        this.$router.push({ path: "/main" });
        this.socketApi.initWebSocket(this.loginForm.userId)
      })
    },

  }
}
</script>
<style lang="less" scoped>
.login_container {
  border-radius: 50%;
  height: 70%;
  width: 50%;
  position: absolute;
  left: 50%;
  top: 50%;
  display: flex;
  float: left;
  transform: translate(-50%, -50%);
  .login_img {
    background-color: blue;
    height: 100%;
    width: 50%;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .login_right {
    background-color: rgb(226, 223, 223);
    height: 100%;
    width: 50%;
    display: table;
    .login_box {
      padding: 20px;
      height: 50%;
      width: 50%;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(0, -50%);
      bottom: 0%;
      box-sizing: border-box;
      .btn_login {
        width: 100%;
      }
      .link_register {
        width: 100%;
      }
    }
  }
}
</style>